package edu.zut.jpa_demo.controller;


import edu.zut.jpa_demo.entity.Account;
import edu.zut.jpa_demo.service.AccountService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
public class AccountController {

    @Resource
    private AccountService accountService;


    /**
     * 模拟转账
     *
     * @param from  转出人
     * @param to    转入人
     * @param money 金额
     * @return
     */
    @GetMapping("/transfer")
    public String transfer(String from, String to, double money) {
        //查询账户
        Account account = accountService.queryByName(from);
        if (Objects.nonNull(account) && account.getBalance() >= money) {
            Account to_account = accountService.queryByName(to);
            if (Objects.nonNull(to_account)) {
                accountService.transfer(account, to_account, money);
                return "转账成功";
            } else {
                return "转入账户不存在";
            }
        } else {
            return "转出账户不存在或余额不足";
        }
    }
}
